package com.junior.fixclub.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.junior.fixclub.R;
import com.junior.fixclub.model.AppInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AppsActivity extends Activity {

    private ListView appList;
    private List<AppInfo> data;
    private AppAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_list);

        //初始化成员变量
        appList = (ListView) findViewById(R.id.app_list);
        data = getAllAppInfos();
        adapter = new AppAdapter();
        //显示列表
        appList.setAdapter(adapter);
    }


    class AppAdapter extends BaseAdapter {

        private Map<Integer,Boolean> map=new HashMap<>();
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        //返回带数据当前行的Item视图对象
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null) {
                convertView = View.inflate(AppsActivity.this, R.layout.app_item, null);
            }

            AppInfo appInfo = data.get(position);

            ImageView imageView = convertView.findViewById(R.id.iv_item_icon);
            TextView textView = convertView.findViewById(R.id.tv_item_name);
            CheckBox checkBox = convertView.findViewById(R.id.tv_item_check);

            imageView.setImageDrawable(appInfo.getIcon());
            textView.setText(appInfo.getAppName());

            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (checkBox.isChecked()){
                        map.put(position,true);
                    }else {
                        map.remove(position);
                    }
                }
            });
            if(map!=null&&map.containsKey(position)){
                checkBox.setChecked(true);
            }else {
                checkBox.setChecked(false);
            }
            return convertView;
        }

    }

    protected List<AppInfo> getAllAppInfos() {
        List<AppInfo> list = new ArrayList<AppInfo>();
        PackageManager packageManager = getPackageManager();
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> ResolveInfos = packageManager.queryIntentActivities(intent, 0);
        for (ResolveInfo ri : ResolveInfos) {
            String packageName = ri.activityInfo.packageName;
            Drawable appIcon = ri.loadIcon(packageManager);
            String appName = ri.loadLabel(packageManager).toString();
            AppInfo appInfo = new AppInfo(appIcon, appName, packageName);
            list.add(appInfo);
        }
        return list;
    }

}